<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/vue-2.4.0.js"></script>
    <!-- <script src="./lib/bootstrap-3.3.7.css"></script> -->
    <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
    <style>
        
    </style>
</head>
<body>
    <div id="app">

        <div class="panel-prmary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                 <label >
                     id:
                     <input type="text" class="form-control" v-model="id">
                 </label>
                 <label >
                        Name:
                        <input type="text" class="form-control" v-model="name" @keyup.f2="add">
                </label>
                <input type="button" value="添加" class="btn btn-primary" @click="add">
                <label >
                        搜索关键字:
                        <input type="text" class="form-control" v-model="keywords" v-focus>
                </label>
            </div>
        </div>
        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for ="item in search(keywords)" :key="item.id">
                    <td>{{item.id}}</td>
                    <td v-text="item.name"></td>
                    <td v-font>{{item.ctime}} </td>
                    <td><a href="" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </tbody>
        </table>
        
    </div>
    <script>
        Vue.directive('focus',{
            bind:function(el,binding){
                // el.focus()
                // console.log(binding.value);
                
                el.style.color='blue'
                // el.style.color = binding.value
            },
            inserted:function(el){
                el.focus()

            },
            updated:function(el){
                
            }
        })
        directives:{
            'font':{
                bind:function(el){
                        el.style.color= 'blue'
                }
            }
        }
        Vue.config.keyCodes.f2=113
        var vm = new Vue({
            el:'#app',
            data:{
                id:'',
                name:'',
                keywords:'',
                list:[
                   {id:1,name:'奔驰',ctime: new Date()},
                   {id:2,name:'宝马',ctime:new Date()} 
                ]
            },
            methods:{
               add(){
                   var car = {id:this.id,name:this.name,ctime:new Date()}
                   this.list.push(car)
                   this.id = this.name = null
               },
               del(id){
                   this.list.some((item , i)=>{
                        if(item.id ==id){
                            this.list.splice(i,1)
                            return true
                        }
                   })
            //    var index = this.list.findIndex(item=>{
            //         if(item.id == id){
            //             return true
            //         }
            //     })
                // console.log(index)
                // this.list.splice(index,1)
               },
               search(keywords){
                //    var newList=[]
                //    this.list.forEach(item => {
                //        if(item.name.indexOf(keywords)!=-1){
                //            newList.push(item)
                //        } 
                //    });
                //    return newList
                return this.list.filter(item=>{
                    // if(item.name.indexOf(keywords))
                    if(item.name.includes(keywords)){
                        return item
                    }
                })
                // return newList
               }
            }
        })
    </script>
</body>
</html>